package com.example.homeworkrabbitmq.service.impl;

import com.example.homeworkrabbitmq.entity.Course;
import com.example.homeworkrabbitmq.entity.Order;
import com.example.homeworkrabbitmq.entity.User;
import com.example.homeworkrabbitmq.enums.OrderStatus;
import com.example.homeworkrabbitmq.repository.CourseRepository;
import com.example.homeworkrabbitmq.repository.OrderRepository;
import com.example.homeworkrabbitmq.repository.UserRepository;
import com.example.homeworkrabbitmq.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.IDUtils;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private CourseRepository courseRepository;

    @Override
    @Transactional
    public Order createOrder(String username,Integer courseId){
        Order order = new Order();
        User user = userRepository.findUserByUsername(username);
        Course course = courseRepository.findById(courseId).get();
        order.setUser(user);
        order.setCourse(course);
        order.setOrderNo(IDUtils.randomUUID());
        //添加订单状态
        order.setOrderStatus(OrderStatus.TO_BE_PAYED);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        //保存订单
        return orderRepository.save(order);
    }

    /**
     * 支付订单
     * @param orderNo
     */
    @Transactional
    @Override
    public int payOrder(String orderNo){
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setOrderStatus(OrderStatus.PAYED);
        order.setUpdateTime(new Date());
      return orderRepository.updateStatus(order);
    }

    /**
     * 设置订单超时
     * @param orderNo
     * @return
     */
    @Transactional
    @Override
    public int timeoutOrder(String orderNo){
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setOrderStatus(OrderStatus.TIMEOUT);
        order.setUpdateTime(new Date());
        return orderRepository.updateStatus(order);
    }

    /**
     * 条件查询
     * @param order
     * @return
     */
    @Override
    public Order findOne(Order order){
        Example<Order> example = Example.of(order);
       return orderRepository.findOne(example).orElse(null);
    }

    /**
     * 更新订单状态
     * @param order
     * @return
     */
    @Override
    @Transactional
    public int updateOrderStatus(Order order){
        return orderRepository.updateStatus(order);
    }

    /**
     * 查询失败的历史账单
     * @param username
     * @return
     */
    @Override
    public List<Order> failOrderList(String username){
        Order order = new Order();
        order.setOrderStatus(OrderStatus.TIMEOUT);
        User user = userRepository.findUserByUsername(username);
        order.setUser(user);
        Example<Order> example = Example.of(order);
        List<Order> failOrders = orderRepository.findAll(example, Sort.by(Sort.Order.desc("updateTime")));
        return failOrders;
    }

}
